package com.imonsoft.sqm.web.basedata;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.imonsoft.common.constant.CCommon;
import com.imonsoft.common.constant.ConstantDict;
import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.httpModel.ReturnMessage;
import com.imonsoft.common.util.AjaxUtil;
import com.imonsoft.common.util.JsonUtil;
import com.imonsoft.sqm.pojo.basedata.Dictionary;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.service.basedata.DictionaryService;
import com.imonsoft.sqm.service.basedata.SupplierService;
import com.imonsoft.sqm.validator.basedata.SupplierValidator;

@Controller
@RequestMapping("/basedata/supplier")
public class SupplierController {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private SupplierService supplierService;
	@Autowired
	private SupplierValidator supplierValidator;
	@Autowired
	private DictionaryService dictionaryService;

	@RequestMapping("/listUI.do")
	public void listUI(ModelMap model) {
		List<Dictionary> dictBaseDataStatusList = dictionaryService.findDictionaryByType(ConstantDict.BASE_DATA_STATUS);
		model.put("dictBaseDataStatusList", dictBaseDataStatusList);
		List<Dictionary> dictMaterialGroupList = dictionaryService.findDictionaryByType(ConstantDict.SUPPLIER_MATERIAL_GROUP);
		model.put("dictMaterialGroupList", dictMaterialGroupList);
	}

	@RequestMapping(value = "/ajaxList.do", method = RequestMethod.POST)
	public void ajaxList(HttpServletResponse response, Pager pager, Supplier supplier) {
		DataGrid<Supplier> dataGrid = new DataGrid<Supplier>();
		try {
			dataGrid = supplierService.getDataGrid(supplier, pager);
		} catch (SystemException se) {
			logger.info(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(dataGrid));
	}

	@RequestMapping("/addUI.do")
	public void addUI(ModelMap model) {
		List<Dictionary> dictMaterialGroupList = dictionaryService.findDictionaryByType(ConstantDict.SUPPLIER_MATERIAL_GROUP);
		model.put("dictMaterialGroupList", dictMaterialGroupList);
	}

	@RequestMapping(value = "/ajaxAdd.do", method = RequestMethod.POST)
	public void ajaxAdd(HttpServletResponse response, Supplier supplier, BindingResult result) {
		ReturnMessage message = new ReturnMessage();
		try {
			supplierValidator.methodAddValidate(supplier, result);
			if (result.hasErrors()) {
				message.setObj(supplierValidator.getErrors(result));
			} else {
				supplierService.insert(supplier);
				message.setSuccess(true);
				message.setMsg(ConstantSqm.OPERATE_SUCCESS);
			}
		} catch (SystemException se) {
			logger.info(se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}

	@RequestMapping(value = "/modifyUI.do")
	public void modifyUI(ModelMap model, Long id) {
		if (null != id) {
			Supplier supplier = supplierService.get(id);
			model.put("supplier", supplier);
		} else {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
		List<Dictionary> dictMaterialGroupList = dictionaryService.findDictionaryByType(ConstantDict.SUPPLIER_MATERIAL_GROUP);
		model.put("dictMaterialGroupList", dictMaterialGroupList);
	}

	@RequestMapping(value = "/ajaxModify.do", method = RequestMethod.POST)
	public void ajaxModify(HttpServletResponse response, Supplier supplier, BindingResult result) {
		ReturnMessage message = new ReturnMessage();
		try {
			supplierValidator.methodModifyValidate(supplier, result);
			if (result.hasErrors()) {
				message.setObj(supplierValidator.getErrors(result));
			} else {
				supplierService.update(supplier);
				message.setSuccess(true);
				message.setMsg(ConstantSqm.OPERATE_SUCCESS);
			}

		} catch (SystemException se) {
			logger.info(se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}

	@RequestMapping(value = "/ajaxStop.do", method = RequestMethod.POST)
	public void ajaxStop(HttpServletResponse response, @RequestParam("ids") List<Long> idList) {
		ReturnMessage message = new ReturnMessage();
		try {
			supplierService.stopSupplier(idList);
			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);
		} catch (SystemException se) {
			logger.info(se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}

	@RequestMapping(value = "/ajaxStart.do", method = RequestMethod.POST)
	public void ajaxStart(HttpServletResponse response, @RequestParam("ids") List<Long> idList) {
		ReturnMessage message = new ReturnMessage();
		try {
			supplierService.startSupplier(idList);
			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);
		} catch (SystemException se) {
			logger.info(se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
    @RequestMapping("/dialogUI.do")
    public void dialogUI(ModelMap model) {
        List<Dictionary> dictBaseDataStatusList = dictionaryService.findDictionaryByType(ConstantDict.BASE_DATA_STATUS);
        model.put("dictBaseDataStatusList", dictBaseDataStatusList);
    }
}
